﻿using Coldairarrow.Entity.DataView;
using Coldairarrow.Entity.DTO;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Coldairarrow.Business.DataView
{
    public partial class blade_visualBusiness : BaseBusiness<blade_visual>, Iblade_visualBusiness, ITransientDependency
    {
        public blade_visualBusiness(IDbAccessor db)
            : base(db)
        {
        }

        #region 外部接口

        public async Task<PageResult<blade_visualDTO>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var where = LinqHelper.True<blade_visualDTO>();
            var search = input.Search;
            Expression<Func<blade_visual, blade_visual_category, blade_visualDTO>>
                select = (a, b) => new blade_visualDTO { Typename = b.category_key };
            select = select.BuildExtendSelectExpre();
            var q = from a in GetIQueryable().AsExpandable()
                    join b in Db.GetIQueryable<blade_visual_category>() on a.category equals b.category_value into ab
                    from b in ab.DefaultIfEmpty()
                    select @select.Invoke(a, b);
            ////筛选
            //if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            //{
            //    var newWhere = DynamicExpressionParser.ParseLambda<blade_visualDTO, bool>(
            //        ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
            //    where = where.And(newWhere);
            //}
            if (!search.Condition.IsNullOrEmpty())
            {
                q = q.Where(x => x.category == search.Condition);
            }
            if (!search.Keyword.IsNullOrEmpty())
            {
                var keyword = $"%{search.Keyword}%";
                q = q.Where(x =>
                          EF.Functions.Like(x.title, keyword));
            }
            return await q.Where(where).GetPageResultAsync(input);
        }

        public async Task<blade_visual> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        public async Task AddDataAsync(blade_visual data)
        {
            await InsertAsync(data);
        }

        public async Task UpdateDataAsync(blade_visual data)
        {
            await UpdateAsync(data);
        }
        [Transactional]
        public async Task DeleteDataAsync(List<string> ids)
        {
            //删除子表数据
            string itemid = "";
            foreach (var item in ids)
            {
                itemid += "'" + item + "',";
            }
            itemid = itemid.Substring(0, itemid.Length - 1);
            await ExecuteSqlAsync("delete from blade_visual_config  where visual_id in (" + itemid + ")");
            await DeleteAsync(ids);
        }

        public async Task<List<blade_visual>> GetDataListAsync()
        {
            var q = GetIQueryable();

            return await q.ToListAsync();
        }

        #endregion

        #region 私有成员

        #endregion
    }
}